package com.itcast.demo_one

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.itcast.demo_one.fragment.DetailFragment
import com.itcast.demo_one.fragment.ListFragment

class MainActivity : AppCompatActivity(), ListFragment.OnItemClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        /*
        * 如果是第一次加载Activity
        * 为 null 时，表示 Activity 是首次创建;
        * 不为 null 时，表示 Activity 不是首次创建，即返回栈中存在前一个Activity实例
        */
        if (savedInstanceState == null){
            // 加载ListFragment
            supportFragmentManager
                .beginTransaction()
                .add(R.id.fragment_container, ListFragment())
                .commit()
        }

    }

    override fun onItemClick(position: Int) {
       supportFragmentManager
           .beginTransaction()
           .replace(R.id.fragment_container, DetailFragment())
           .addToBackStack(null)
           .commit()
    }

}